package com.blog.boot.service.system;

import com.blog.boot.api.entity.ArticleEnity;
import com.blog.boot.api.system.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ArticleEnity getArticleByid(Long id){
        Query query = new Query(Criteria.where("_id").is(id));
        return mongoTemplate.findOne(query,ArticleEnity.class);
    }
    @Override
    public List<ArticleEnity> getListArticleByModule(String module){
        Query query = new Query(Criteria.where("module").is(module));
        return mongoTemplate.find(query,ArticleEnity.class);
    }

    @Override
    public void upArticleByid(Long id, Integer rank, String field) {
        Update update = new Update();
        update.set(field,rank);
        mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(id)),update,ArticleEnity.class);
    }
}
